/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceLoginClient;

import WebServiceModel.GamingStateModel;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

/**
 *
 * @author Peach
 */
public class LoginClient {
    public LoginClient() {
	try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = "loginServer";
	methodLogin = "login";
	methodLogout = "logout";
    }

    public LoginClient(String URI, String methodLoginString,
        String methodLogoutString) {
	try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = URI;
	methodLogin = methodLoginString;
        methodLogout = methodLogoutString;
    }

    public int login(String detail,String userId) {
	Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodLogin);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("detail", String.class, detail, null));
        params.addElement(new Parameter("userId", String.class, userId, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                //System.out.println(result.getValue());
                return (Integer)result.getValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
        return 0;
    }

    public int logout(String userId) {
	Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodLogout);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("userId", String.class, userId, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                //System.out.println(result.getValue());
                return (Integer)result.getValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
        return 0;
    }


    public static void main(String args[]) throws Exception {
	LoginClient test=new LoginClient();
        for(int i=0;i<=10;i++){
            System.out.println(test.login("2",i+2+""));
        }
    }
    

    private URL url;
    private String uri;
    private String methodLogin;
    private String methodLogout;
}
